//
//  SYNumKeyboard.m
//  SYKeyboard
//
//  Created by hc on 2019/3/1.
//  Copyright © 2019 hc. All rights reserved.
//

#import "SYNumKeyboard.h"

@interface SYNumKeyboard ()

@property (nonatomic, strong) NSMutableArray *btnArray;
@property (nonatomic, strong) UIButton *letterBtn;      //  切换键盘btn

@end

@implementation SYNumKeyboard

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _btnArray = [NSMutableArray array];
        _isNumKeyboard = NO;
        [self setupSubviews];
    }
    return self;
}

- (void)changeNum {
    NSMutableArray *numArray = [NSMutableArray arrayWithArray:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]];
    for (int i = 0; i < 10; i++) {
        UIButton *button = _btnArray[i];
        int loc = arc4random_uniform((int)numArray.count);
        [button setTitle:[numArray objectAtIndex:loc] forState:UIControlStateNormal];
        [numArray removeObjectAtIndex:loc];
    }
}

- (void)sortNum {
    NSMutableArray *numArray = [NSMutableArray arrayWithArray:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0", nil]];
    for (int i = 0; i < 10; i++) {
        UIButton *button = _btnArray[i];
        int loc = 0;
        [button setTitle:[numArray objectAtIndex:loc] forState:UIControlStateNormal];
        [numArray removeObjectAtIndex:loc];
    }
}

/**
 快速创建数字键盘
 
 @param delegate 代理对象
 @return 键盘对象
 */
+ (SYNumKeyboard *)creatNumberKeyboardWithDelegateTarget:(id<SYBaseKeyboardDelegate>)delegate {
    SYNumKeyboard * keyboard = [[SYNumKeyboard alloc] initWithFrame:CGRectMake(0, 0, SYKeyboardScreenWidth, SYKeyboardHeight)];
    keyboard.delegate = delegate;
    return keyboard;
}

- (void)setIsNumKeyboard:(BOOL)isNumKeyboard {
    _isNumKeyboard = isNumKeyboard;
    
    self.letterBtn.hidden = isNumKeyboard;
}

- (void)setupSubviews {
    NSMutableArray *numArray = [NSMutableArray arrayWithArray:[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0", nil]];
    int row = 4;
    int coloumn = 3;
    int width = 5;
    int height = 10;
    
    CGFloat keyWidth = (SYKeyboardScreenWidth - width*4) / coloumn;
    CGFloat keyHeight = (SYKeyboardHeight - height*5) / row;
    
    for (int i = 0; i < 12; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i % coloumn *(keyWidth + width) + width, i / coloumn *(keyHeight + height) + height, keyWidth, keyHeight)];
        button.tag = i;
        [button setBackgroundImage:[[UIImage imageNamed:@"zimubaikuang"] stretchableImageWithLeftCapWidth:10 topCapHeight:10] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [self addSubview:button];
        if (i == 9 || i == 11) {
            button.titleLabel.font = [UIFont systemFontOfSize:13.0f];
            if (i == 9) {
                [button setTitle:@"ABC" forState:UIControlStateNormal];
                self.letterBtn = button;
            }else {
                [button setBackgroundImage:nil forState:UIControlStateNormal];
                [button setImage:[UIImage imageNamed:@"icon_del_black"] forState:UIControlStateNormal];
            }
        } else {
            [_btnArray addObject:button];
            int loc = 0;
            [button setTitle:[numArray objectAtIndex:loc]forState:UIControlStateNormal];
            [numArray removeObjectAtIndex:loc];
        }
    }
}

- (void)buttonTap:(UIButton *)sender{
    switch (sender.tag) {
        case 9:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"字母" keyType:LetterSwitchKeyType keyboardType:NumberKeyboard];
            }
        }
            break;
        case 11:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:@"删除" keyType:DeleteKeyType keyboardType:NumberKeyboard];
            }
        }
            break;
            
        default:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:)]) {
                [self.delegate pressKey:sender.titleLabel.text keyType:NumberKeyType keyboardType:NumberKeyboard];
            }
        }
            break;
    }

}

@end
